package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class IndexController {

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;

    @Reference
    private ISearchService searchService;

    @RequestMapping("")
    public String index(Model model ){


        //游记推荐： tcs
        model.addAttribute("tcs",travelCommendService.getTop5());

        //攻略推荐:sc
        model.addAttribute("sc",strategyCommendService.getTop5().get(0));


        return "index/index";
    }

    //游记查询
    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo")TravelQuery qo){

        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "index/travelPageTpl";
    }





    //搜索操作
    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo")SearchQueryObject qo){

        switch (qo.getType()){
            //目的地操作
            case SearchQueryObject.SEARCHE_TYPE_DEST:
                return searchDest(model, qo);
            case SearchQueryObject.SEARCHE_TYPE_STRATEGY:
                return searchStrategy(model, qo);
            case SearchQueryObject.SEARCHE_TYPE_TRAVEL:
                return searchTravel(model, qo);
            case SearchQueryObject.SEARCHE_TYPE_USER:
                return searchUser(model, qo);
            default: return  searchAll(model, qo);
        }
    }
    //查目的地
    private String searchDest(Model model,SearchQueryObject qo){
        //1:根据传入keyword查询es是否存在目的地数据

        DestinationTemplate dest = destinationSearchService.findByName(qo.getKeyword());
        if(dest != null){
            //2:如果存在, 查询该目的地下所有攻略, 游记, 用户
            List<StrategyTemplate> sts = strategySearchService.findByDestName(qo.getKeyword());
            List<TravelTemplate> ts = travelSearchService.findByDestName(qo.getKeyword());
            List<UserInfoTemplate> us = userInfoSearchService.findByDestName(qo.getKeyword());

            SearchResult data = new SearchResult();
            data.setUsers(us);
            data.setStrategys(sts);
            data.setTravels(ts);
            data.setTotal(sts.size() + ts.size() + us.size() + 0L);

            model.addAttribute("data", data);

        }
        //3:如果不存在, 页面提示信息
        model.addAttribute("dest", dest);
        return "index/searchDest";
    }
    //查攻略
    private String searchStrategy(Model model,SearchQueryObject qo){
        //全文搜索与高亮显示
        Page<StrategyTemplate> stPage = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME,
                StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title","subTitle","summary");
        model.addAttribute("page", stPage);
        return "index/searchStrategy";
    }
    //查游记
    private String searchTravel(Model model,SearchQueryObject qo){
        Page<TravelTemplate> trPage = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,
                TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title","summary");
        model.addAttribute("page", trPage);
        return "index/searchTravel";
    }
    //查用户
    private String searchUser(Model model,SearchQueryObject qo){
        Page<UserInfoTemplate> uPage = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,
                UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname","info","destName");
        model.addAttribute("page", uPage);
        return "index/searchUser";
    }
    //查所有
    private String searchAll(Model model,SearchQueryObject qo){

        //全文搜索与高亮显示
        Page<StrategyTemplate> stPage = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME,
                StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title","subTitle","summary");

        Page<TravelTemplate> trPage = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,
                TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title","summary");

        Page<UserInfoTemplate> uPage = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,
                UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname","info","destName");

        Page<DestinationTemplate> dPage = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME,
                DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name","info");


        SearchResult data = new SearchResult();
        data.setTravels(trPage.getContent());
        data.setStrategys(stPage.getContent());
        data.setUsers(uPage.getContent());
        data.setDests(dPage.getContent());
        data.setTotal(trPage.getTotalElements() + stPage.getTotalElements() + uPage.getTotalElements() + dPage.getTotalElements());

        model.addAttribute("data", data);

        return "index/searchAll";
    }

















}
